package uk.co.chrisdadswell.radioreminder;

import java.io.File;

/**
 * Created with IntelliJ IDEA.
 * User: Christian Dadswell
 * Date: 29/03/13
 * Time: 11:07
 */

public class functions_io {

    private static String className = "functions_io";

    // CHECK FOR SDCARD
    public static boolean IsSDPresent() {
        return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    }

    // CHECK FOR WORKING DIRECTORIES, CREATE IF NECESSARY
    public static boolean CreateADirectory(String fileOrFolder){
        File file=new File(fileOrFolder);
        if(IsSDPresent()){
            boolean exists = file.exists(); {
                if (!exists) {
                    // It returns false if File or directory does not exist and creates directory
                    try {
                        File sddir = new File(fileOrFolder);
                        if(sddir.mkdir()) {
                            System.out.println(className + ": Directory " + fileOrFolder + " created successfully");
                        }else{
                        }
                    }
                    finally{}
                }else{
                    System.out.println(className + ": Directory " + fileOrFolder + " already exists");
                }
            }
            return exists;
        }else{
            System.out.println("MAIN: /sdcard not mounted");
        }
        return true;
    }

    public static boolean DeleteAFile(String fileToDelete) {
        File file=new File(fileToDelete);
        boolean exists = file.exists();
        if(!exists) {
            // It returns false if File does not exist
            System.out.println("MAIN: File: " + fileToDelete + " does NOT exist");
        }else{
            // It returns true if File exists
            System.out.println("MAIN: File: " + fileToDelete + " exists");
            file.delete();
            return exists;
        }
        return false;
    }

    public static boolean FileOrDirectoryExists(String fileOrDirectoryToLookFor) {
        File file=new File(fileOrDirectoryToLookFor);
        boolean exists = file.exists();
        if(!exists) {
            // It returns false if File or directory does not exist
            System.out.println("MAIN: File or Directory: " + fileOrDirectoryToLookFor + " does NOT exist");
        }else{
            // It returns true if File or directory exists
            System.out.println("MAIN: File or Directory: " + fileOrDirectoryToLookFor + " exists");
            return exists;
        }
        return false;
    }

    public static int CountFilesInDirectory(File directoryToLookAt) {
        int count = 0;
        for (File file : directoryToLookAt.listFiles()) {
            if (file.isFile()) {
                count++;
            }
            if (file.isDirectory()) {
                count += CountFilesInDirectory(file);
            }
        }
        return count;
    }


}


